<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
	    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
	    <title>新人礼包</title>
	    <meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">

	    <script src="../js/mui.min.js"></script>
	    <script src="../js/jquery.3.2.1.js"></script>
	    <link href="../css/mui.min.css" rel="stylesheet"/>
	    <link href="../css/mui_common.css" rel="stylesheet"/>
	    <link href="../css/index/wcd.css" rel="stylesheet"/>
		<script type="text/javascript" charset="utf-8">
	      	mui.init();
	   </script>
	   <style>
	   	.content {
            font-size: 20px;
            width: 100%;
            /*height: ;*/
           height: auto;
            overflow: auto;
            padding: 5px;
        }
        .content li {
            margin-top: 10px;
            padding-left: 10px;
            width: 100%;
            display: block;
            clear: both;
            overflow: hidden;
        }
        .content li img {
            float: left;
            width: 3rem;
            height: 3rem;
        }
        .content li span{
            background: #7cfc00;
            padding: 10px;
            border-radius: 10px;
            float: left;
            margin: 6px 10px 0 10px;
            max-width: 310px;
        }
        .content li img.imgleft {
            float: left;
        }
        .content li img.imgright {
            float: right;
        }
        .content li span.spanleft {
            float: left;
            background: #fff;
        }
        .content li span.spanright {
            float: right;background: #fff;
		    border-radius: 8px;
		    padding: 0.6rem;
		    line-height: 1.2rem;
        }
        
        .bottom {
            display: none;
            position: fixed;
            bottom: 0;
            padding: 10px;
        }
       
        .bottom span {
            display: inline-block;
            width: 62px;
            height: 48px;
            background: #ccc;
            font-weight: 900;
            line-height: 45px;
            cursor: pointer;
            text-align: center;
            position: absolute;
            right: 10px;
            border-radius: 6px;
        }
        .bottom span:hover {
            color: #fff;
            background: #999;
        }
        #user_face_icon {
            /*display: none;*/
            background: red;
            width: 2.5rem;
            height: 2.5rem;
            border-radius: 30px;
            position: absolute;
            bottom: 3.8rem;
            left: 0.5rem;
            cursor: pointer;
            overflow: hidden;
            z-index: 2;
        }
        #user_face_icon img {
            width: 2.5rem;
            height: 2.5rem;
        }
        .con{
        	position: fixed;
        	bottom: 0;
        }
	   </style>
	</head>
	<body id="wcd">
		<header class="mui-bar mui-bar-nav fl">
		    <a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
		    <h1 class="mui-title">搭配师小Y</h1>
		</header>
		<div class="mui-content">
		  	<div class="people">
		  		<div class="img">
		  			<img src="../img/index/wcd/wcd_tx.png">
		  		</div>
		  		<div class="que">妞儿~你希望小Y为你推荐什么样的上衣呢？</div>
		  	</div>
		  	<div class="people1">
		  		<div class="que1">必买清单</div>
		  		<div class="img">
		  			<img src="../img/index/wcd/wcd_tx.png">
		  		</div>
		  	</div>
		  	<ul class="content">
			</ul>
			<div class="a">
				<a>怎么搭</a>
				<a>帮我挑</a>
				<a>场合穿搭</a>
				<a>穿啥？</a>
			</div>
			<p class="bottom">
				<div id="user_face_icon">
				    <img src="../img/index/wcd/wcd_tx.png" alt="">
				</div>
				
				<div class="con">
					<a id="head" >&#xe650;</a>
					<input id="text" type="text" placeholder="说点什么吧" class="text"/>
					<input id="btn" type="submit" class="sub" />
				</div>
			</p>
		</div>
		
		<script>
			
			window.onload = function(){
            var arrIcon = ['../img/index/wcd/wcd_tx.png','../img/index/wcd/wcd_tx.png'];
            var num = 0;     //控制头像改变  
            var iNow = -1;    //用来累加改变左右浮动  
            var icon = document.getElementById('user_face_icon').getElementsByTagName('img');
            var btn = document.getElementById('btn');
            var text = document.getElementById('text');
            var content = document.getElementsByTagName('ul')[0];
           
            var img = content.getElementsByTagName('img');
            var span = content.getElementsByTagName('span');

				
            icon[0].onclick = function(){
                if(num==0){
                    this.src = arrIcon[1];
                    num = 1;
                }else if(num==1){
                    this.src = arrIcon[0];
                    num = 0;
                }
            }
            btn.onclick = function(){
                if(text.value ==''){
                    alert('不能发送空消息');
                }else {
                    content.innerHTML += '<li><img src="'+arrIcon[num]+'"><span>'+text.value+'</span></li>';
                    iNow++;
                    if(num==0){
                        img[iNow].className += 'imgright';
                        span[iNow].className += 'spanright';
                    }else {
                        img[iNow].className += 'imgleft';
                        span[iNow].className += 'spanleft';
                    }
                    text.value = '';
                    // 内容过多时,将滚动条放置到最底端
                    contentcontent.scrollTop=content.scrollHeight;
                }
            }
        }
			
			document.getElementById('head').addEventListener('tap', function() {
			    if(mui.os.plus) {
			        var a = [{
			            title: "拍照"
			    }, {
			        title: "从手机相册选择"
			    }];
			    plus.nativeUI.actionSheet({
//			        title: "修改用户头像",
			        cancel: "取消",
			        buttons: a
			    }, function(b) { /*actionSheet 按钮点击事件*/
			        switch(b.index) {
			        case 0:
			            break;
			        case 1:
			            getImage(); /*拍照*/
			            break;
			        case 2:
			            galleryImg(); /*打开相册*/
			            break;
			        default:
			            break;
			        }
			    })
			    }
			}, false);
			//拍照
			function getImage() {
			    var cmr = plus.camera.getCamera();
			    var res = cmr.supportedImageResolutions[0];
			    var fmt = cmr.supportedImageFormats[0];
			    cmr.captureImage(function(path) {
			        //plus.io.resolveLocalFileSystemURL(path, function(entry) {  
			    plus.io.resolveLocalFileSystemURL(path, function(entry) {
			        var localUrl = entry.toLocalURL();
			        uploadHead(localUrl + "?version=" + new Date().getTime());
			    }, function(err) {
			        console.error("拍照失败：" + err.message);
			    }, {
			        index: 1
			    });
			    });
			} 
			//本地相册选择
			function galleryImg() {
			    plus.gallery.pick(function(a) {
			    plus.io.resolveLocalFileSystemURL(a, function(entry) {
			        plus.io.resolveLocalFileSystemURL("_doc/", function(root) {
			        root.getFile("head.png", {}, function(file) {
			            //文件已存在
			            file.remove(function() {
			            console.log("file remove success");
			            entry.copyTo(root, 'head.png', function(e) {
			                var e = e.fullPath + "?version=" + new Date().getTime();
			                uploadHead(e); /*上传图片*/
			                //变更大图预览的src
			                //目前仅有一张图片，暂时如此处理，后续需要通过标准组件实现
			                },function(e) {
			                            console.log('copy image fail:' + e.message);
			            });
			            }, function() {
			            console.log("delete image fail:" + e.message);
			            });
			        }, function() {
			            //文件不存在
			            entry.copyTo(root, 'head.png', function(e) {
			            var path = e.fullPath + "?version=" + new Date().getTime();
			            uploadHead(path); /*上传图片*/
			            },function(e) {
			            console.log('copy image fail:' + e.message);
			            });
			        });
			        }, function(e) {
			        console.log("get _www folder fail");
			        })
			    }, function(e) {
			        console.log("读取拍照文件错误：" + e.message);
			    });
			    }, function(a) {}, {
			    filter: "image"
			    })
			};
			
			//上传头像图片
			function uploadHead(imgPath) {
			    var image = new Image();
			    image.src = imgPath;
			    image.onload = function() {
			    var imgData = getBase64Image(image);
			    console.log(imgData);
			    /*在这里调用上传接口*/
			    //mui.ajax("图片上传接口", {
			        //data: {
			        //img: imgData
			        //},
			        //dataType: 'json',
			        //type: 'post',
			        //timeout: 10000,
			        //success: function(data) {
			        //mui.toast('上传成功',{
			            //duration:'long',
			            //type:'div'
			        //});
			                //document.getElementById('head-img').src = imgPath;
			        //document.getElementById('head-img1').src = imgPath;
			        //document.getElementById('head-img2').src=imgPath;
			        //}, 
			            //error: function(xhr, type, errorThrown) {
			        //mui.toast('网络异常，请稍后再试！');
			        //}
			    //});
			    }
			}
			//将图片压缩转成base64
			function getBase64Image(img) {
			    var canvas = document.createElement("canvas");
			    var width = img.width;
			    var height = img.height;
			    // calculate the width and height, constraining the proportions
			    if(width > height) {
			    if(width > 100) {
			        height = Math.round(height *= 100 / width);
			        width = 100;
			    }
			    } else {
			    if(height > 100) {
			        width = Math.round(width *= 100 / height);
			        height = 100;
			    }
			    }
			    canvas.width = width; /*设置新的图片的宽度*/
			    canvas.height = height; /*设置新的图片的长度*/
			    var ctx = canvas.getContext("2d");
			    ctx.drawImage(img, 0, 0, width, height); /*绘图*/
			    var dataURL = canvas.toDataURL("image/png", 0.8);
			    return dataURL.replace("data:image/png;base64,", "");
			} 
		</script>
	</body>
</html>
